package com.logansoft.zhxypkoa.repository.sys;

import java.util.List;
import java.util.Optional;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.data.rest.core.annotation.RestResource;
import org.springframework.transaction.annotation.Transactional;

import com.logansoft.zhxypkoa.domain.sys.Organization;

@RepositoryRestResource(collectionResourceRel = "organizations", path = "organizations")
public interface OrganizationRepository extends JpaRepository<Organization, String> {
	@Cacheable(value = "organizations", key = "#root.methodName + '-' + #p0") 
	@RestResource(path = "findByParentIdIsNull")
	public Page<Organization> findByParentIdIsNull(Pageable pageable);
	@Cacheable(value = "organizations", key = "#root.methodName + '-' + #p0 + '-' + #p1") 
	@RestResource(path = "findByParentId")
	public Page<Organization> findByParentId(@Param(value = "parentId") String parentId, Pageable pageable);
	@Transactional
	@Modifying
	@RestResource(path = "deleteInIdList")
	@Query("delete from Organization o where id in ?1")
	public int deleteInIdList(@Param(value = "idList") List<String> idList);
	// 使用 pageable 作为键设置缓存
	@Cacheable(value = "organizations", key = "#root.methodName + '-' + #p0") 
	@Override
	Page<Organization> findAll(Pageable pageable);
	@CacheEvict(value = "organizations", key = "#p0.id", allEntries = true)
	@Override
	<S extends Organization> S save(S entity);
	@Cacheable(value = "organizations", key = "#p0") 
	@Override
	Optional<Organization> findById(String id);
	@CacheEvict(value = "organizations", key = "#p0.id", allEntries = true) 
	@Override
	void delete(Organization entity);
}